<?php
namespace Home\Controller;
use Think\Controller;
class UsersController extends Controller {
        public function register(){
        $code=I('post.verifycode');
        if (!$this->check_verify($code)) {
            $this->error('验证码错误');
        }
        if($_FILES['file']['error']==0 ){
            $info=$this->upload();
            $name=$info["file"]["savename"];
        }
        else{
            $name="default.jpg";
        }
        
        $user=D('users');
        if($user->create()){
            $user->photo=$name;
            if(false!==$user->add()){
                $this->success('注册成功!','login');
            }else{$this->error('数据错误');}
        }else{
            $this->error($user->getError());
        }
    }
    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Public/Uploads/photo/'; // 设置附件上传根目录  //  
        $upload->autoSub = false;//关闭子目录，默认true
        //$upload->savePath  =     ''; // 设置附件上传(子)目录，默认目录名为年月日
        $info   =   $upload->upload();  // 上传文件 
        if(!$info) {
            $this->error($upload->getError());    
        }
        else{
            return $info;
        }                    
    }
    public function checkLogin(){
        $code=I('post.verifycode');
        if (!$this->check_verify($code)) {
            $this->error('验证码错误');
        }
        $data['telephone']=trim(I('post.telephone'));
        $data['password']=md5(I('post.password'));
        $usermodel=M('users');
        $rs=$usermodel->where($data)->select();
        if($rs){
            session("tele",$data['telephone']);
            session("alias",$rs['0']["aliasname"]);
            $this->success("登录成功",U("Goods/logindex"));
        }
        else{
            $this->error("用户名或密码错误");
        }
    }

    //

public function checkLogin2(){
        $code=I('post.verifycode');
        if (!$this->check_verify($code)) {
            $this->error('验证码错误');
        }
        session("admin_name",null);
        $data['admin_name']=trim(I('post.admin_name'));
        $data['password']=I('post.password');
        $usermodel=M('admin');
        $rs=$usermodel->where($data)->select();
        if($rs){
            session("admin_name",$data['admin_name']);
            $this->success("登录成功",U("Admin/User/userlist"));
        }
        else{
            $this->error("管理员名或密码错误");
        }
    }
    
    public function verify(){
        $Verify = new \Think\Verify();
        $Verify->entry();
    }
    public function check_verify($code,$id=''){
        $Verify = new \Think\Verify();
        return $Verify->check($code,$id);
    }  
    public function usersinfo(){

        $user = M('users');
        $data['telephone']=$_SESSION['tele'];
        $res = $user->where($data)->find();
        $this->userinfo=$res;
       /* var_dump($res);  */
        $find = M('find');
        $res2 = $find->where($data)->select();  
        $this->findList=$res2;  
       // var_dump($res2);
        //var_dump($findList);
        //echo   $find = M('find')->getLastSql();
        $lost = M('lost');
        $res3 = $lost->where($data)->select();  
        $this->lostList=$res3;  
        //var_dump($res3);
        $this->display();
  }
 
public function editUser()
{       
        $user = M('users');
        $data['telephone']=$_SESSION['tele'];
        /* dump($data);*/
        $res = $user->where($data)->find();
        $this->userinfos=$res;


       $this->display();
}
   public function doUpdate(){
       /* $id=$_GET('id');
        var_dump($id);*/
        $users=M("users");
        $data['telephone']=$_SESSION['tele'];
         // $res = $user->where($data)->find();
        $id=$res['id'];
        var_dump($id);
        if ($users->create()) {
        $data['telephone' ]=I('post.telephone');
        $data['aliasname']=I('post.aliasname');
        $data['email']=I('post.email');


        $reg=$users->where("id=22")->save($data);

        if (false!==$reg) {
            $this->success('修改成功','../Users/usersinfo');
        }
        else
        {
           $this->error('修改错误','../Users/edituser'); 
        }  
}
}


}
?>
